package com.greenantmall.adapter;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.greenantmall.R;
import com.greenantmall.bean.ServerSection;

public class SelectAreaSearchAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mLayoutInflater;
	
	private List<UpTownBean> mDataList;
	
	public SelectAreaSearchAdapter(Context context, List<UpTownBean> dataList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(mContext);
		mDataList = this.sortAddressBeanListToMSpinnerBeanList(dataList);
	}
	
	@Override
	public void notifyDataSetChanged() {
		mDataList = this.sortAddressBeanListToMSpinnerBeanList(mDataList);
		super.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if(mDataList != null) {
			return mDataList.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View child = convertView;
		ViewHolder holder = null;
		if(child == null) {
			child = mLayoutInflater.inflate(R.layout.select_area_list_right_item, null);
			holder = new ViewHolder(child);
			child.setTag(holder);
		}
		else {
			holder = (ViewHolder)child.getTag();
		}
		
		UpTownBean bean = mDataList.get(position);
		holder.getTvDataStr1().setText(bean.serverSection.getUptown());
		Log.d("CEXX", "CEXX ---> bean.serverSection.getUpTown : " + bean.serverSection.getUptown());
        if(bean.flag){
        	holder.getTvDataStr0().setVisibility(View.VISIBLE);
        	holder.getTvDataStr0().setText(bean.dataStr);
        }
        else{
        	holder.getTvDataStr0().setVisibility(View.GONE);
        }
		
		return child;
	}
	
	private class ViewHolder{
        private View mContentView;
        private TextView mTvDataStr0;
        private TextView mTvDataStr1;
        
        public ViewHolder(View contentView){
            mContentView = contentView;
        }
        
        public TextView getTvDataStr0(){
            if(mTvDataStr0 == null){
                mTvDataStr0 = (TextView)mContentView.findViewById(R.id.select_area_list_right_item_tv0);
            }
            return mTvDataStr0;
        }
        
        public TextView getTvDataStr1(){
            if(mTvDataStr1 == null){
                mTvDataStr1 = (TextView)mContentView.findViewById(R.id.select_area_list_right_item_tv1);
            }
            return mTvDataStr1;
        }
    }

    public static class UpTownBean{
        /**
         * 是否显示首字母
         */
        public boolean flag;
        /**
         * 第一个字的首字母
         */
        public String dataStr;
        /**
         * 各个字首字母
         * 例如：中国人 ---> zgr;
         */
        public String firstPinYin;
        /**
         * 显示的数据
         */
        public ServerSection serverSection;
    }
    
    private List<UpTownBean> sortAddressBeanListToMSpinnerBeanList(List<UpTownBean> mspinnerBeanList){
        if(mspinnerBeanList != null && mspinnerBeanList.size()>0){
            for(UpTownBean bean : mspinnerBeanList){
                bean.firstPinYin = cn2FirstSpell(bean.serverSection.getUptown());
                bean.dataStr = "" + bean.firstPinYin.charAt(0);
            }
            
            Collections.sort(mspinnerBeanList, new Comparator<UpTownBean>(){
                @Override
                public int compare(UpTownBean object1, UpTownBean object2)
                {
                    String firstPinYin1 = object1.firstPinYin;
                    String firstPinYin2 = object2.firstPinYin;
                    int size = firstPinYin1.length() < firstPinYin2.length() ? firstPinYin1.length() : firstPinYin2.length();
                    for(int i=0; i<size; i++){
                        if(firstPinYin1.charAt(i) > firstPinYin2.charAt(i)){
                            return 1;
                        }
                        else if(firstPinYin1.charAt(i) < firstPinYin2.charAt(i)){
                            return -1;
                        }
                    }
                    return 0;
                }
            });
            
            int size = mspinnerBeanList.size();
            for(int i=0; i<size; i++){
            	UpTownBean mspinnerB = mspinnerBeanList.get(i);
                if(i == 0){
                    mspinnerB.flag = true;
                }
                else if(!mspinnerB.dataStr.equals(mspinnerBeanList.get(i-1).dataStr)){
                    mspinnerB.flag = true;
                }
                else{
                    mspinnerB.flag = false;
                }
            }
        }
        return mspinnerBeanList;
    }
    
    /**
     * 获取汉字串拼音首字母，英文及特殊字符字符不变
     * @param chinese 汉字串
     * @return 汉语拼音首字母
     */
    private String cn2FirstSpell(String chinese)
    {
        StringBuffer pybf = new StringBuffer();
        char[] arr = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < arr.length; i++)
        {
            Log.d("NEVER", String.valueOf(arr[i]));
            if (arr[i] > 128)
            {
                try
                {
                    String[] _t = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
                    if (_t != null)
                    {
                        pybf.append(_t[0].charAt(0));
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e)
                {
                    e.printStackTrace();
                }
            } else
            {
                pybf.append(arr[i]);
            }
        }
        return pybf.toString().toUpperCase().trim();
    }
}








